package com.lemon.smartsafetynew.ui.adapter.record;

import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lemon.smartsafety.R;
import com.lemon.smartsafety.listener.RecyclerTClickListener;
import com.lemon.smartsafetynew.entity.common.DateTypeEnum;
import com.lemon.smartsafetynew.entity.record.EmergenVo;
import com.lemon.smartsafetynew.util.common.CommonUtil;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import kale.adapter.LemonRcvAdapter;
import kale.adapter.item.AdapterItem;

/**
 * Created by JiangAn on 2018/5/25.
 */

public class EmergencyInfoAdapter extends LemonRcvAdapter<EmergenVo> {
    private RecyclerTClickListener<EmergenVo> mListener;

    public EmergencyInfoAdapter(@Nullable List<EmergenVo> data, RecyclerTClickListener<EmergenVo> mListener) {
        super(data);
        this.mListener = mListener;
    }

    @NonNull
    @Override
    public AdapterItem createItem(Object type) {
        return new EmergencyInfoAdapter.EmergenItem();
    }

    public class EmergenItem implements AdapterItem<EmergenVo> {

        @Bind(R.id.tv_space_name)
        TextView tvSpaceName;
        @Bind(R.id.tv_belong_large_class)
        TextView tvBelongLargeClass;
        @Bind(R.id.tv_belong_small_class)
        TextView tvBelongSmallClass;
        @Bind(R.id.tv_involve_facilities)
        TextView tvInvolveFacilities;
        @Bind(R.id.tv_emergency_number)
        TextView tvEmergencyNumber;
        @Bind(R.id.tv_emergency_status)
        TextView tvEmergencyStatus;
        @Bind(R.id.item_layout)
        LinearLayout itemLayout;
        @Bind(R.id.tv_date)
        TextView tvDate;
        @Bind(R.id.frameLayout)
        FrameLayout frameLayout;

        private EmergenVo data;
        private int pos;

        @Override
        public int getLayoutResId() {
            return R.layout.layout_emergency_item;
        }

        @Override
        public void bindViews(View root) {
            ButterKnife.bind(this, root);
        }

        @Override
        public void setViews() {
            itemLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mListener.onItemClick(data, pos);
                }
            });
        }

        @Override
        public void handleData(EmergenVo emergenVo, int position) {

            data = emergenVo;
            pos = position;

            tvSpaceName.setText(CommonUtil.avoidNullString(emergenVo.getEmergency_equipment_name()));
            tvBelongLargeClass.setText(CommonUtil.avoidNullString(emergenVo.getBigType()));
            tvBelongSmallClass.setText(CommonUtil.avoidNullString(emergenVo.getSmallType()));
            tvInvolveFacilities.setText(CommonUtil.avoidNullString(emergenVo.getProcess_facilities()));
            tvEmergencyNumber.setText(CommonUtil.avoidNullString(emergenVo.getCount()));
            tvEmergencyStatus.setText(CommonUtil.avoidNullString(emergenVo.getStatus()));
            if (emergenVo.getDayStatus() != null) {
                String status = emergenVo.getDayStatus(); //0 正常  1快过期 2已过期
                if (status.equals("0")) {
                    frameLayout.setBackgroundResource(R.mipmap.onecom_stamp1);
                    tvDate.setTextColor(Color.parseColor("#458bd0"));
                } else if (status.equals("1")) {
                    frameLayout.setBackgroundResource(R.mipmap.onecom_stamp1);
                    tvDate.setTextColor(Color.parseColor("#458bd0"));
                } else if (status.equals("2")) {
                    frameLayout.setBackgroundResource(R.mipmap.onecom_stamp3);
                    tvDate.setTextColor(Color.parseColor("#FF0202"));
                }
            }
            if (emergenVo.getEnd_time() != null) {
                tvDate.setText("到期日期\n" + CommonUtil.secondToTimeStr(emergenVo.getEnd_time(), DateTypeEnum.DAY));
            }
        }
    }
}
